<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/24
 * Time: 18:20
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\ExpressService;
use App\Constant\ErrorConstant;

class ExpressController extends BaseController
{
    protected $service;

    public function __construct(Request $request, ExpressService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 快递公司列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listExpress()
    {
        [$flag, $result]    =   $this->service->expressList();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_LIST_ERROR, $result);
        }
    }

    /**
     * 快递公司删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteExpress()
    {
        $validator  =   Validator::make($this->request->post(), [
            'express_id'       =>  'required|integer',
        ], [
            'express_id.required'  =>  '快递公司ID不能为空',
            'express_id.integer'   =>  '快递公司ID必须为整数',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_DELETE_ERROR, $validator->errors()->first());
        }

        $express_id    =   (int) $this->request->post('express_id');

        [$flag, $result]    =   $this->service->expressDelete($express_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_DELETE_ERROR, $result);
        }
    }

    /**
     * 快递公司添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addExpress()
    {
        $validator  =   Validator::make($this->request->all(), [
            'express_name'  =>  'required|string|max:30',
            'short_name'    =>  'string|max:30',
            'express_web'   =>  'string|max:300',
            'express_phone' =>  'string|max:30',
        ], [
            'express_name.required' =>  '快递公司名称不能为空',
            'express_name.string'   =>  '快递公司名称只能为字符',
            'express_name.max'      =>  '快递公司名称过长',

            'short_name.string'     =>  '快递公司简称只能为字符',
            'express_web.string'    =>  '快递公司官网只能为字符',
            'express_phone.string'  =>  '快递公司电话只能为字符',

            'express_phone.max'     =>  '快递公司官网过长',
            'express_web.max'       =>  '快递公司电话过长',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_ADD_ERROR, $validator->errors()->first());
        }

        $params =   $this->request->post();
        [$flag, $result]    =   $this->service->expressAdd($params);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_ADD_ERROR, $result);
        }
    }

    /**
     * 快递公司编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function editExpress()
    {
        $validator  =   Validator::make($this->request->all(), [
            'express_id'    =>  'required|integer|exists:mini_express,id',
            'express_name'  =>  'required|string|max:30',
            'short_name'    =>  'string|max:30',
            'express_web'   =>  'string|max:300',
            'express_phone' =>  'string|max:30',
        ], [
            'express_id.required'   =>  '快递公司ID不能为空',
            'express_id.integer'    =>  '快递公司ID必须为整数',
            'express_id.exists'     =>  '快递公司不存在',
            'express_name.required' =>  '快递公司名称不能为空',
            'express_name.string'   =>  '快递公司名称只能为字符',
            'express_name.max'      =>  '快递公司名称过长',

            'short_name.string'     =>  '快递公司简称只能为字符',
            'express_web.string'    =>  '快递公司官网只能为字符',
            'express_phone.string'  =>  '快递公司电话只能为字符',

            'express_phone.max'     =>  '快递公司官网过长',
            'express_web.max'       =>  '快递公司电话过长',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_ADD_ERROR, $validator->errors()->first());
        }

        $params     =   $this->request->post();
        $express_id =   (int) $this->request->post('express_id');
        [$flag, $result]    =   $this->service->expressEdit($params, $express_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_EXPRESS_ADD_ERROR, $result);
        }
    }

}
